/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.restaurant.jsf.util;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;

@FacesValidator("passwordRepeat")
public class PasswordRepeatValidator implements Validator{
    
        @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {

        try{
        UIInput passwordComponent = (UIInput) component.getAttributes().get("passwordComponent");
        String password = passwordComponent.getValue().toString();
        String confirmPassword = value.toString();

        if (confirmPassword != null && !confirmPassword.equals(password)) {

            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Different password","Different password");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);
        }

        }
        catch (NullPointerException e)
        {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Different password","Different password");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);

        }

    }
    
}
